using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.Remoting.Channels;
using System.Text;
using NUnit.Framework;
using Universe.Remoting.Extensions;

namespace Universe.Remoting.Tests
{
    [TestFixture]
    public class Test
    {
        [SetUp]
        public void MySetup()
        {
            // is every test running via dedicated AppDomain
            MyServerInit.Run();
            MyClientInit.Run();
            AppDomain.CurrentDomain.UnhandledException +=
                delegate(object sender, UnhandledExceptionEventArgs e)
                    {
                        Trace.WriteLine("CurrentDomain UnhandledException" + Environment.NewLine + e.ExceptionObject);
                    };

            TrafficDefaultInfo.TurnWritingOn();
        }

        [Test]
        public void TestSingleton()
        {
            TrafficDefaultInfo.ServerTraffic.Clear();
            TrafficDefaultInfo.ClientTraffic.Clear();
            IMySingleton s = ClientRepository.Instance.GetService<IMySingleton>();
            MyData d = s.NextCounter();
            Assert.IsTrue(d.IsMarshaled, "Return value Is marshaled via Remoting");
            d = s.NextCounter();
            Assert.AreEqual(2, d.Counter, "Two calls of Singleton NextCounter");
            Assert.AreEqual(3, TrafficDefaultInfo.ServerTraffic.Total.Requests, "Server Requests");
            Assert.AreEqual(123456, MyServerInit.LastTransparentValue, "Transparent Delivery");
        }

        [Test]
        public void TestSingleCall()
        {
            TrafficDefaultInfo.ServerTraffic.Clear();
            TrafficDefaultInfo.ClientTraffic.Clear();
            IMySingleCall s = ClientRepository.Instance.GetService<IMySingleCall>();
            MyData d = s.NextCounter();
            Assert.IsTrue(d.IsMarshaled, "Is Return value marshaled via Remoting");
            d = s.NextCounter();
            Assert.AreEqual(1, d.Counter, "Two calls of SingleCall NextCounter");
            Assert.AreEqual(3, TrafficDefaultInfo.ServerTraffic.Total.Requests, "Server Requests");
            Assert.AreEqual(123456, MyServerInit.LastTransparentValue, "Transparent Delivery");
        }

        [TearDown]
        public void MyTearDown()
        {
            MyServerInit.Close();
            TrafficDefaultInfo.Flush();
            TrafficDefaultInfo.TurnWritingOff();
        }

    }
}
